using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIQuestGet : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIQuestGet),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIQuestGet
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textTitle { protected set; get; }
            public UnityEngine.UI.ExLoopVerticalScrollRect loopVerticalScrollRect { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textDes { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv0);
                this.btnClose = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textTitle", out var __tbv1);
                this.textTitle = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("loopVerticalScrollRect", out var __tbv2);
                this.loopVerticalScrollRect = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textDes", out var __tbv3);
                this.textDes = __tbv3;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        LoopScrollAdapter<UIN_OneItemGet> loopCell = null;

        UD_QuestGet udQuest => _data as UD_QuestGet;

        protected UIB_UIQuestGet ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIQuestGet();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.loopCell = this.InitLoopScroll<UIN_OneItemGet>(this.ui.loopVerticalScrollRect, this.OnLoopItemChanged);
            this.ui.btnClose.onClick.AddListener(this.Close);
        }

        protected override void OnDispose()
        {

        }

        protected override void OnShow(UIBaseData data)
        {
            UD_QuestGet udQuest = data as UD_QuestGet;
            if (udQuest == null)
            {
                // this.Dispose();
                return;
            }
            var cfg = udQuest.cfgData;
            int cnt = udQuest.rewards.Count;

            this.ui.textDes.text = cfg.Description;
            this.ui.textTitle.text = cfg.Name;
            this.loopCell.RefillCells(cnt);
        }

        protected override void OnHide()
        {

        }

        void OnLoopItemChanged(UIN_OneItemGet item, int index)
        {
            var svData = this.udQuest.rewards[index];
            item.Show(svData);
        }
    }
}
